{% extends 'base.html' %}

{% block title %}用户列表 - Django RBAC 项目{% endblock %}

{% block content %}
    <div class="container mx-auto px-4 py-8">
        <h1 class="text-2xl font-bold mb-6">用户列表</h1>

        <div class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead>
                    <tr>
                        <th class="text-left py-2">用户名</th>
                        <th class="text-left py-2">邮箱</th>
                        <th class="text-left py-2">账号状态</th>
                        <th class="text-left py-2">操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    {% for user in users %}
                        <tr class="border-t">
                            <td class="py-2">{{ user.username }}</td>
                            <td class="py-2">{{ user.email }}</td>
                            <td class="py-2">
                            <span class="{% if user.is_active %}text-green-500{% else %}text-red-500{% endif %} ml-2">
                                {% if user.is_active %}激活{% else %}禁用{% endif %}
                            </span>
                            </td>
                            <td class="py-2">
                                {% if user.id != request.user.id %}
                                    <div class="flex space-x-2">
                                        {% if can_detail %}
                                            <a href="{% url 'user_detail' user.id %}"
                                               class="text-blue-500 hover:text-blue-800">
                                                查看
                                            </a>
                                        {% endif %}
                                        {% if can_edit %}
                                            <a href="{% url 'user_edit' user.id %}"
                                               class="text-green-500 hover:text-green-800">
                                                编辑
                                            </a>
                                        {% endif %}
                                        {% if can_delete %}
                                            <a href="{% url 'user_delete' user.id %}"
                                               class="text-red-500 hover:text-red-800"
                                               onclick="return confirm('确定要删除用户`{{ user.username }}`吗？')">
                                                删除
                                            </a>
                                        {% endif %}
                                        {% if can_assign %}
                                            <a href="{% url 'assign_role_to_user' user.id %}"
                                               class="text-blue-500 hover:text-blue-800">
                                                分配角色
                                            </a>
                                        {% endif %}
                                    </div>
                                {% else %}

                                    <span class="text-gray-500">当前登录用户</span>

                                {% endif %}
                            </td>
                        </tr>
                    {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>

        {% if users.has_other_pages %}
            <div class="mt-4 flex justify-center">
                <nav class="inline-flex rounded-md shadow">
                    {% if users.has_previous %}
                        <a href="?page={{ users.previous_page_number }}"
                           class="px-3 py-2 rounded-l-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50">
                            上一页
                        </a>
                    {% endif %}

                    {% for i in users.paginator.page_range %}
                        {% if users.number == i %}
                            <span class="px-3 py-2 border border-gray-300 bg-blue-50 text-sm font-medium text-blue-600">
                {{ i }}
            </span>
                        {% else %}
                            <a href="?page={{ i }}"
                               class="px-3 py-2 border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50">
                                {{ i }}
                            </a>
                        {% endif %}
                    {% endfor %}

                    {% if users.has_next %}
                        <a href="?page={{ users.next_page_number }}"
                           class="px-3 py-2 rounded-r-md border border-gray-300 bg-white text-sm font-medium text-gray-500 hover:bg-gray-50">
                            下一页
                        </a>
                    {% endif %}
                </nav>
            </div>
        {% endif %}
    </div>
{% endblock %}

